﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SASystem.SAServiceRef;

namespace SASystem
{
    public partial class EditStandardFrm : Form
    {
        public EditStandardFrm()
        {
            InitializeComponent();
        }

        private void EditStandardFrm_Load(object sender, EventArgs e)
        {
            DoRefresh();
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;

            StandardInfo newStandardInfo = new StandardInfo();
            newStandardInfo.Name = this.textBox_standardName.Text;
            newStandardInfo.Description = this.richTextBox_standardDescription.Text;

            bool ret = SABLL.AddStandard(newStandardInfo);

            if (!ret)
            {
                SABLL.ShowErrMsg("添加失败！" + SABLL.GetLastErrMsg());
                return;
            }
            else
            {
                SABLL.ShowInfoMsg("添加成功！");
                DoRefresh();
            }
        }

        private void listView_standardList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView_standardList.SelectedItems == null || this.listView_standardList.SelectedItems.Count == 0)
            {
                return;
            }
            //get current selected item
            ListViewItem curItem = this.listView_standardList.SelectedItems[0];
            if (curItem == null)
            {
                return;
            }

            //get standard info. use standardName, becuase new added item, the tag is null.
            String standardName = curItem.Text;
            StandardInfo curStandardInfo = SABLL.GetStandardByName(standardName);

            if (curStandardInfo == null)
            {
                SABLL.DoAlert(); return;
            }

            this.textBox_standardName.Text = curStandardInfo.Name;
            this.textBox_standardName.Tag = curStandardInfo.StandardId;
            this.richTextBox_standardDescription.Text = curStandardInfo.Description;
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;

            //get standardId.
            int standardId = Convert.ToInt32(this.textBox_standardName.Tag);
            StandardInfo updateStandardInfo = new StandardInfo();
            updateStandardInfo.StandardId = standardId;
            updateStandardInfo.Name = this.textBox_standardName.Text;
            updateStandardInfo.Description = this.richTextBox_standardDescription.Text;

            bool result = SABLL.UpdateStandard(updateStandardInfo);

            if (!result)
            {
                SABLL.DoAlert(); return;
            }
            else
            {
                SABLL.ShowInfoMsg("修改成功!");
                DoRefresh();
            }
        }

        private void DoRefresh()
        {
            SABLL.InitStandardListView(this.listView_standardList);
            this.textBox_standardName.Text = "";
            this.richTextBox_standardDescription.Text = "";
            this.textBox_standardName.Tag = null;
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (this.listView_standardList.SelectedItems == null || this.listView_standardList.SelectedItems.Count == 0)
                return;

            ListViewItem curItem = this.listView_standardList.SelectedItems[0];

            string standardName = curItem.Text;

            //check whether there are families for this standard.
            //
            int standardId = Convert.ToInt32(this.textBox_standardName.Tag);
            List<FamilyInfo> famInfos = SABLL.GetFamiliesByStandardId(standardId);
            if (famInfos != null && famInfos.Count > 0)
            {
                SABLL.ShowInfoMsg(String.Format("无法删除：[{0}]，仍有{1}个零部件属于标准：[{0}]！", standardName, famInfos.Count));
                return;
            }

            string msg = String.Format("确定要删除标准：{0}", standardName);
            if (SABLL.ShowConfirmMsg(msg) == DialogResult.Yes)
            {
                bool result = SABLL.DeleteStandard(standardName);
                if (!result)
                {
                    SABLL.DoAlert(); return;
                }
                else
                {
                    SABLL.ShowInfoMsg("删除成功！");
                    DoRefresh();
                }
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool CheckInput()
        {
            if (String.IsNullOrEmpty(this.textBox_standardName.Text.Trim()))
            {
                SABLL.ShowInfoMsg(@"标准的名字不能为空！");
                return false;
            }
            return true;
        }
    }
}
